﻿/// <reference path="dataPersister.js" />
/// <reference path="../../libs/_references.js" />
/// <reference path="../../libs/underscore.js" />

window.viewModelsFactory = (function () {

    var dataPersister = persisters.getPersister("api/");

    function getLoginViewModel(successCallback) {
        var viewModel = {
            username: "",
            password: "",

            login: function () {
                username = this.get("username");
                password = this.get("password");
                dataPersister.users.login(username, password)
                    .then(function () {
                        if (successCallback()) {
                            successCallback();
                        }
                    });
            },
            logout: function () {
                dataPersister.users.logout()
                    .then(function () {
                        if (successCallback()) {
                            successCallback();
                        }
                    });
            }
        };

        return kendo.observable(viewModel);
    }

    function getRegisterViewModel(successCallback) {
        var viewModel = {
            username: "",
            password: "",
            email: "",

            register: function () {
                username = this.get("username");
                password = this.get("password");
                email = this.get("email");

                dataPersister.users.register(username, password, email)
                    .then(function () {
                        if (successCallback()) {
                            successCallback();
                        }
                    });
            },

            logout: function () {
                dataPersister.users.logout()
                    .then(function () {
                        if (successCallback()) {
                            successCallback();
                        }
                    });
            }
        };

        return kendo.observable(viewModel);
    }

    function createSingleToDoViewModel(listId, successCallback) {
        var viewModel = {
            text: "",

            createSingle: function () {
                text = this.get("text");

                dataPersister.todos.createSingleTodo(listId, text)
                    .then(function () {
                        if (successCallback()) {
                            successCallback();
                        }
                    });
            }
        };

        return kendo.observable(viewModel);
    }

    function getTodosViewModel() {
        return dataPersister.todos.all()
            .then(function (todos) {

                var viewModel = {
                    todos: todos,
                    message: ""
                };

                return kendo.observable(viewModel);
            });
    }

    function getTodosByIdViewModel(id) {
        return dataPersister.todos.getById(id)
            .then(function (todosData) {
                var viewModel = {
                    todos: todosData
                };

                return kendo.observable(viewModel);
            });
    }

    function createAppointmentViewModel() {
        var viewModel = {
            subject: "",
            description: "",
            appointmentDate: "",
            duration: "",

            create: function () {
                subject = this.get("subject");
                description = this.get("description");
                appointmentDate = this.get("appointmentDate");
                duration = this.get("duration");

                dataPersister.appointments.create(subject, description, appointmentDate, duration);
            }
        };

        return kendo.observable(viewModel);
    }

    function getAppointmentsViewModel() {
        return dataPersister.appointments.all()
            .then(function (appointments) {
                var viewModel = {
                    appointments: appointments,
                    messages: ""
                };

                return kendo.observable(viewModel);
            });
    }

    function getCommingAppointmentsViewModel() {
        return dataPersister.appointments.getAllComming()
            .then(function (appointments) {
                var viewModel = {
                    appointments: appointments,
                    messages: ""
                };

                return kendo.observable(viewModel);
            });
    }

    function getTodaysAppointmentsViewModel() {
        return dataPersister.appointments.getTodays()
            .then(function (appointments) {
                var viewModel = {
                    appointments: appointments,
                    messages: ""
                };

                return kendo.observable(viewModel);
            });
    }

    function getCurrentAppointmentsViewModel() {
        return dataPersister.appointments.getCurrent()
            .then(function (appointments) {
                var viewModel = {
                    appointments: appointments,
                    messages: ""
                };

                return kendo.observable(viewModel);
            });
    }

    function getAppointmetsByDateViewModel(date) {
        return dataPersister.appointments.getByDate(date)
            .then(function (appointments) {
                var viewModel = {
                    appointments: appointments,
                    messages: ""
                };

                return kendo.observable(viewModel);
            });
    }

    function createTodoListViewModel() {
        var viewModel = {
            title: "",
            todosList: "",

            create: function () {
                var todos = [];
                var todosList = this.get("todosList");
                var subTodosSplitted = todosList.split(",");
                for (var i = 0; i < subTodosSplitted.length; i++) {
                    todos.push({ text: subTodosSplitted[i] });
                }

                title = this.get("title");
                dataPersister.todos.create(title, todos);
            }
        };

        return kendo.observable(viewModel);
    }

    return {
        getLoginViewModel: getLoginViewModel,
        getRegisterViewModel: getRegisterViewModel,
        getTodosViewModel: getTodosViewModel,
        getTodosByIdViewModel: getTodosByIdViewModel,
        getAppointmentsViewModel: getAppointmentsViewModel,
        getCommingAppointmentsViewModel: getCommingAppointmentsViewModel,
        getTodaysAppointmentsViewModel: getTodaysAppointmentsViewModel,
        getCurrentAppointmentsViewModel: getCurrentAppointmentsViewModel,
        createAppointmentViewModel: createAppointmentViewModel,
        createTodoListViewModel: createTodoListViewModel,
        createSingleToDoViewModel: createSingleToDoViewModel,
        getAppointmetsByDateViewModel: getAppointmetsByDateViewModel
    };

}());